﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace ly.Web.QianTai
{
    public partial class addWork : System.Web.UI.Page
    {
        ly.Model.WorkInfo workModel = new Model.WorkInfo();
        ly.BLL.WorkInfo workBll = new BLL.WorkInfo();
        ly.Model.UserInfo userModel = new Model.UserInfo();
        ly.BLL.UserInfo userBll = new BLL.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initLogin();

            }
        }
        public void initLogin()//初始化用户登录
        {

            if (Session["username"] != null)//判断用户是否登录
            {
                lblUserName.Text = Session["username"].ToString();
                pnlLogin.Visible = false;
                pnlLoginOut.Visible = true;
            }
            else
            {
                Response.Write("<script>alert('不登录无法新建作品,点击确定后隔一秒重新调回主页');</script>");
                Response.Write("<meta http-equiv='refresh' content='1; url=index.aspx' />");
            }
        }




              protected void btntianjia_Click(object sender, EventArgs e)
              {

                  string path = "";
                  if (FileUpload.HasFile)
                  {
                      string fileExrensio = System.IO.Path.GetExtension(FileUpload.FileName).ToLower();//ToLower转化为小写
                      string UploadURL = Server.MapPath("~/files/");//上传的目录
                      //if (fileExrensio == ".xlsx" || fileExrensio == ".xls")//判断文件类型
                      //{
                      try
                      {
                          if (!System.IO.Directory.Exists(UploadURL))//判断文件夹是否已经存在
                          {
                              System.IO.Directory.CreateDirectory(UploadURL);//创建文件夹
                          }
                          string filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                              DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + System.IO.Path.GetFileName(FileUpload.FileName).ToString();
                          path = UploadURL + filename + fileExrensio;
                          //string filename = Path.GetFileName(FileUpload.FileName);
                          this.FileUpload.PostedFile.SaveAs(path);  //保存上传的文件
                          string pathworkp = filename + fileExrensio;
                          workModel.WorkPicture = pathworkp;
                          //上传成功
                          lblmsg.Text = "上传成功";
                      }
                      catch
                      {
                          lblmsg.Text = "上传发生错误，传输失败！";
                          return;
                      }
                      //}

                  }
                  else
                  {
                      lblmsg.Text = "请选择导入文件！";
                      return;
                  }



                  //string path = "";
                  //string fileExrensio = System.IO.Path.GetExtension(FileUpload.FileName).ToLower();//ToLower转化为小写
                  //string UploadURL = Server.MapPath("~/files/");//上传的目录
                  //string filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                  //                DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + System.IO.Path.GetFileName(FileUpload.FileName).ToString();
                  //path = UploadURL + filename + fileExrensio;
                  workModel.WorkName = txtworkname.Text.Trim();
                  workModel.UserName = txtusername.Text.Trim();
                  workModel.UploadTime = Convert.ToDateTime(startTime.Value.Trim());
                  workModel.WorkIntroduction = FCKeditor1.Value.Trim();
                  //workModel.WorkPicture = path;
                  workModel.ActivityID = Convert.ToInt16(ddlact.SelectedValue.Trim());
                  int cou = workBll.Add(workModel);
                  if (cou > 0)
                  {
                      ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功');</script>");
                  }
                  else
                  {
                      ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败');</script>");
                  }
              }




    }
}